package com.atguigu.spzx;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Map;
import java.util.Objects;

@Data
@Accessors(chain = true) //启动链式调用
public class R {
    private Integer code;
    private String message;
    private Map<String, Object> data;

    //提供静态工具方法初始化R对象
    //默认成功
    public static R ok(Object data) {
        R r= R.setResult(200, "SUCCESS");
        r.setData((Map<String, Object>) data);
        return r;

    }

    //默认失败
    public static R fail(){
        return R.setResult(-1,"UNKNOWN ERROR");
    }

    //自定义失败
    public static R setResult(Integer code,String message){
        R r = new R();
//        r.code=code;
//        r.message=message;
        r.setCode(code);
        r.setMessage(message);
        return r;
    }

    //提供自定义的链式调用方法修改其他的属性值
    public R data(Object data){
        this.data = (Map<String, Object>) data;
        return this;
    }
    public R code(Integer code){
        this.code=code;
        return this;
    }
    public R message(String message){
        this.message=message;
        return this;
    }
}
